<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/27
 * Time: 10:45
 */

namespace Admin\Controller;

use Think\Controller;

class FkcbController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('fkcb');
    }

    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);
            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $ssyf = $year . $month;
                $where['yf'] =$ssyf;
            }

            //当月成本类型列表
            $lx_arr = $this->db->where($where)->getField('lx', true);
            $lx_arr = array_values(array_unique($lx_arr));

            //当月对应客户列表
            $dykh_arr = $this->db->where($where)->getField('dykh', true);
            $dykh_arr = array_values(array_unique($dykh_arr));

            $dykh = I('dykh');
            if (!empty($dykh)) {  //对应客户
                $where['dykh'] = I('dykh');
            }

            $lx = I('lx');
            if (!empty($lx)) { //成本类型
                $where['lx'] = I('lx');
            }

            if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示上月的数据
                $last_m = date("Ym",strtotime(date("Ym01",time()))-8600);;//默认情况下显示上月的数据
                $where['yf'] = $last_m;
            }
            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc")->select();

        $this->assign('lx_arr', $lx_arr);//当月成本类型列表
        $this->assign('dykh_arr', $dykh_arr);//当月对应客户列表
        $this->assign('lsts', $lsts);//列表数据

        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['ktrq'] = date('Ymd', strtotime($data['ktrq']));
                $data['jsrq'] = date('Ymd', strtotime($data['jsrq']));
                $data['jfrq'] = date('Ymd', strtotime($data['jfrq']));
                $data['rqqj'] = $data['jfrq'] . "-" . $data['jsrq'];
                $data['status'] = 1;
                $data['ys']=$data['yfk']+$data['tz'];
            }
            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['ktrq'] = date('Ymd', strtotime($data['ktrq']));
                $data['jsrq'] = date('Ymd', strtotime($data['jsrq']));
                $data['jfrq'] = date('Ymd', strtotime($data['jfrq']));
                $data['rqqj'] = $data['jfrq'] . "-" . $data['jsrq'];
                $data['status'] = 1;
                $data['ys']=$data['yfk']+$data['tz'];
            }
            $rid = $this->db->save($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $this->db->where(array('id' => $id))->delete();
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }
}